package net.aetherial.context.grounding.utilities;

import java.io.*;
import java.util.*;

/**
 * A utility class for calculating the frequencies of words and phrases using the frequencies found in the British National Corpus.
 */

public class EnglishWordFrequencies 
{
	private static HashMap<String, Integer> freqDict;
	private static int averageFreq;
	private static int total;
	
	/**
	 * Returns the probability of a given phrase. Probability is calculated by multiplying the probabilities of the words in the phrase.
	 * 
	 * @param phrase	String representing the phrase
	 * @return			Predicted probability of the phrase occurring in conversation.
	 */
	
	public static double getFrequency (String phrase)
	{
		double n = 1;
		double d = 1;
		
		if (freqDict == null)
			initFreqDict ();
		
		for (String tok: phrase.split (" "))
		{
			Integer count = freqDict.get (tok);
		
			if (count != null)
				n *= count.doubleValue ();
			else
				n *= averageFreq;
			
			d *= total;
		}
		
		return n / d;
	}

	/**
	 * Returns the average frequency of all words in the corpus.
	 * 
	 * @return	Average frequency of all words in the corpus.
	 */
	
	public static int getAverage ()
	{
		if (freqDict == null)
			initFreqDict ();
		
		return averageFreq;
	}
	
	/**
	 * Initializes the frequency table by reading the word frequencies from a file and generating data structures to support frequency queries.
	 */
	
	private static void initFreqDict() 
	{
		ArrayList<String> validPrefixes = new ArrayList<String> ();
		
		String[] symbols = {	"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
								"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

		for (String symbol: symbols)
			validPrefixes.add (symbol);

		freqDict = new HashMap<String, Integer> ();

		String path = System.getProperty ("english.frequency.file");
		
		if (path == null)
			path = "/Users/cjkarr/Desktop/english.freq";
		
		try
		{
			BufferedReader reader = new BufferedReader (new FileReader (path));
		
			String line = null;
			
			total = 0;
			
			while ((line = reader.readLine ()) != null)
			{
				String[] toks = line.split (" ");

				if (toks[1].startsWith ("&") || toks[1].startsWith ("*") || toks[1].startsWith ("-") || toks[1].startsWith ("+") || toks[1].startsWith ("/"))
				{
					
				}
				else if (!(validPrefixes.contains (toks[1].substring(0, 1))))
				{
				
				}
				else
				{
					Integer count = freqDict.get (toks[1]);
					
					int instanceCount = Integer.parseInt (toks[0]);

					if (count != null)
						count += instanceCount;
					else
						count = 0;

					total += instanceCount;

					freqDict.put (toks[1], count);
				}
			}
			
			averageFreq = freqDict.size () / total;
		}
		catch (IOException e)
		{
			e.printStackTrace ();
		}
	}
}
